package com.example.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import com.example.activity.ServiceActivity;
import com.example.studyapplication.R;

/**
 * @Description:
 * @Author: 葛浩杰
 * @Version: 1.0 2021/1/12
 */
public class TestService extends Service {

    private CloseReceiver closeReceiver;
    private LocalBinder localBinder = new LocalBinder();

    /**
     * 创建binder对象，返回给客户端即Activity使用，提供数据交换的接口
     */
    public class LocalBinder extends Binder {
        //声明一个方法，getService（提供给客户端调用）
        public TestService getService(){
            return TestService.this;
        }

    }

    /**
     * 把binder类返回给客户端
     * @param intent
     * @return
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("TestService","onBind");
        return localBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("TestService","onCreate");
        //注册关闭广播
        IntentFilter intentFilter2 = new IntentFilter();
        closeReceiver = new CloseReceiver();
        intentFilter2.addAction("com.close.alarm");
        this.registerReceiver(closeReceiver,intentFilter2);

        bindNoti();
    }

    private void bindNoti() {
        // 在API11之后构建Notification的方式
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
        Intent nfIntent = new Intent(this, TestService.class);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, nfIntent, 0))// 设置PendingIntent
                .setContentTitle("睡眠数据") // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
                .setContentText("正在检测分贝和重力加速度") // 设置上下文内容
                .setWhen(System.currentTimeMillis()); // 设置该通知发生的时间

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //修改安卓8.1以上系统报错
            NotificationChannel notificationChannel = new NotificationChannel("111", "sleepd", NotificationManager.IMPORTANCE_MIN);
            notificationChannel.enableLights(false);//如果使用中的设备支持通知灯，则说明此通知通道是否应显示灯
            notificationChannel.setShowBadge(false);//是否显示角标
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(notificationChannel);
            builder.setChannelId("111");
        }
        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音
        startForeground(13691, notification);// 开始前台服务
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("TestService","onStartCommand");
        return START_STICKY;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("TestService","onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(closeReceiver!=null){
            this.unregisterReceiver(closeReceiver);
        }
        Log.e("TestService","关闭service");
    }

    public class CloseReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            stopForeground(false);//false可以被手动划掉
            stopSelf();//关闭服务和通知
        }
    }

    public int getData(){
        return 520;
    }
}
